<?php
require_once(APPPATH."models/base.php");

class Menu_Item_Type
{
	const Link = 'link';
	const Page = 'page';
	const Article_Class = 'article_class';
	const Page_Class = 'page_class';
	const Link_Class = 'link_class';
	const Guest_Book = 'guest_book';
	// const Product = 'product';
} 

class menu_item_model extends base_model
{	
	private $model_caches = Array();
	
	public function get_relation_model()
	{
		if(!isset($this->model_caches[$this->id]))
		{
			$manager_class = $this->type."_manager";
            $this->load->model($manager_class);
			$model_caches[$this->id] = $this->$manager_class->get($this->key_id);
		}
		return $model_caches[$this->id];
	}

	public function get_title()
	{
		if(!empty($this->name)) return $this->name; 
		if($this->type=="guest_book")
		{
			return "留言板";
		}
		if(!empty($this->type))
		{ 
			//return $manager_class;
			$model = $this->get_relation_model();
			return $model->title;   
		}
	}
	
	public function get_items()
	{
		$items = Array();
		$this->db->order_by('order asc,id asc')  ;  
		$db_items =
			$this->db->get_where('menu_item',array('parent_id'=>$this->id))->result();
		foreach($db_items as $db_item)
		{
			$items[] = $this->convert($db_item,'menu_item_model');
		}
		// print_r($items);
		return $items; 
	}
	
	public function get_href()
	{
		$href = "";
		if($this->type==Menu_Item_Type::Article_Class)
		{
			//$href = _U("article/index?".$this->key);
			$href = _U("article/index?class-id=".$this->key_id);
		}
		if($this->type==Menu_Item_Type::Page)
		{
			//$href = _U("page/show?".$this->key);
			$href = _U("page/show?id=".$this->key_id);
		}
		if($this->type==Menu_Item_Type::Link)
		{
			$model = $this->get_relation_model();
			$url = $model->url;
			if(preg_match("/^~\\/(.*)$/i",$url))
			{
				$url = preg_replace("/^~\\/(.*)$/i","\\1",$url);
				$url = _U($url) ; 
			}
			$href = $url ;   
		} 
		if($this->type==Menu_Item_Type::Guest_Book)
		{
			//$href = _U("article/index?".$this->key);
			$href = _U("guest_book/show"); 
		}
		return $href;  
	}
	
	
	
	
	private function get_children_by_id($id)
	{
		$menu_items = array();
		$this->db->order_by('order asc,id asc')  ;
		$db_menu_items =
			$this->db->get_where('menu_item',array('parent_id'=>$id))->result();
		// print_r($db_article_classs);exit;
		foreach($db_menu_items as $db_menu_item)
		{
			$menu_items[] = $this->convert($db_menu_item);
		}
		return $menu_items;
	}
	
	public function get_parent()
	{
		return $this->menu_manager->get_item($this->parent_id);
	}
	
	public function get_children()
	{
		return $this->get_children_by_id($this->id);
	}
	
	public function get_brothers()
	{
		// echo $this->parent_id;exit;
		return $this->get_children_by_id($this->parent_id);
	}
	
	private $descendants = array();
	private function bl_descendant($item)
	{
		$children = $item->children;
		//p_r($children);exit;
	
		foreach($children as $child)
		{
			$this->descendants[] = $child;
			//p_r($ds);exit;
			if($child->children != null)
			{
				$this->bl_descendant($child);
			}
		}
	}
	public function get_descendant()
	{
		$this->descendants = array();
		//echo "ddd";
		$this->bl_descendant($this);
		// p_r($this->descendants);exit;
		return $this->descendants;
	}
	
	public function get_ancestors()
	{
		$class = $this;
		$parent = $class->parent;
		$as = array();
		while ($parent->id!=0)
		{
			$as[] = $parent;
			$parent = $parent->parent;
		}
		return array_reverse($as);
	}
	
}  

class menu_model 
{
	public $items;
	
	function merge($ob)
	{
		foreach($ob as $key => $value)
		{
			$this->$key = $value;
		}
	} 
}

class menu_manager extends base_manager
{
	public function get($menu_name)
	{
		$lang = &get_instance()->get_lang();  
		$items = Array();
		$this->db->order_by('order asc,id asc')  ; 
		$db_items = 
			$this->db->get_where('menu_item',
				array(
					'language'=>$lang,
					'menu_name'=>$menu_name,
					'parent_id'=>0
				)
		)->result();
		
		foreach($db_items as $db_item)
		{
			$items[] = $this->convert($db_item,'menu_item_model'); 
		}
		// print_r($items);exit;
		$menu = new menu_model();
		$menu->items = $items;
		return $menu; 
	}
	
	public function get_item($id)
	{
		return $this->convert($this->db->get_where('menu_item',array('id'=>$id))->row(),'menu_item_model'); 
	}
	
	public function update_item($post_item,$params)
	{
		$this->db->update('menu_item',$post_item, $params);
	}
	
	public function insert_item($post_item)
	{
		$this->db->insert('menu_item',$post_item); 
		return $this->db->insert_id ; 
	}
	
	private function bl_delete_item($id)
	{
		$menu_item = $this->get_item($id);
		if($menu_item->id=='') return;
		if(sizeof($menu_item->items)>0)
		{
			foreach($menu_item->items as $item)
			{
				$this->bl_delete_item($item->id);
			}
		}
		$this->db->delete('menu_item',array('id'=>$id));
	}
	
	public function delete_item($id)
	{
		$this->db->trans_begin();
		$this->bl_delete_item($id); 
		if ($this->db->trans_status() === FALSE)
		{
			$this->db->trans_rollback();
		}
		else
		{
			$this->db->trans_commit();
		}     
	}
	
	

	
	public function moveto($parent_id,$id)
	{
		$target = $this->get_item($id);
		if(empty($target->id)) return;
		$target_descedant = $target->descendant;
		foreach($target_descedant as $descedant)
		{
			if($descedant->id == $parent_id) return ;
		}
		
		if($parent_id!=$id)
		{
			$this->db->update('menu_item',array("parent_id"=>$parent_id), array("id"=>$id)) ; 
		}
	}
}

?>